var ArtificialRecord = function() {
	return {
		datatable : null,
		init : function() {
			$("#startTime").datetimepicker({lang:'ch',timepicker:false, format:"Y-m-d"});
			$("#endTime").datetimepicker({lang:'ch',timepicker:false, format:"Y-m-d"});
			$("#clockTime").datetimepicker({lang:'ch',timepicker:true, format:"Y-m-d H:i:s"});
			this.initSearch();
			this.initListener();
		},
		initSearch : function() {
			if (!jQuery().dataTable) {
				return;
			}
			var artificialRecordListTable = $("#artificialRecordListTable").dataTable({
				"sAjaxSource" : ctx + "/artificialRecord/search?now=" + new Date().getTime(),
				/* 使用post方式 */
				"fnServerData" : function(sSource, aoData, fnCallback) {
					var postData = {page : 1,start : 0,rows : 10,params : {}};
					if($('#userIds').val()){
						postData.params.userIds = $('#userIds').val();
						console.log($('#userIds').val());				
					}
					
					if($('#startTime').val()){
						postData.params.startTime = $('#startTime').val();
					}
					
					if($('#endTime').val()){
						postData.params.endTime = $('#endTime').val();
					}
					
					// 参数
					for (var i = 0, size = aoData.length; i < size; i++) {
						if (aoData[i].name == 'iDisplayLength') {
							postData.rows = aoData[i].value;
						} else if (aoData[i].name == 'iDisplayStart') {
							postData.start = aoData[i].value;
						} else if (aoData[i].name == 'sEcho') {
							postData.page = aoData[i].value;
						}
					}

					$.ajax({
						"dataType" : 'json',
						"cache": false,
						"type" : "POST",
						"url" : sSource,
						"data" : postData,
						"success" : function(data, textStatus, jqXHR){
							fnCallback(data.data, textStatus, jqXHR);
						}
					});
				},
				"bAutoWidth":false,
				"aoColumnDefs" : [
						{
							"data" : "index",
							"aTargets" : [ 0 ]
						},{
							"data" : "emplName",
							"aTargets" : [ 1 ],
							'bSortable': true
						},{
							"data" : "emplCode",
							"aTargets" : [ 2 ]
						},{
							"data" : "clockIp",
							"aTargets" : [ 3 ]
						},{
							"data" : "manHoursTypeName",
							"aTargets" : [ 4 ]
						},{
							"aTargets" : [ 5 ],
							"mRender" : function(data, type, full) {
								return new Date(full.clockTime).format("yyyy-MM-dd hh:mm:ss");
							}
						},{
							"data" : "clockPlace",
							"aTargets" : [ 6 ]
						},{
							"data" : "attnTypeName",
							"aTargets" : [ 7 ]
						},{
							"aTargets" : [ 8 ],
							"mRender" : function(data, type, full) {
								return '<button title="'+messages.public_button_del+'" class="btn btn-sm-table btn-danger m-r-5" name="del" target_id="'+full.id+'">删除</button>';
							}
						}]
			});
			this.datatable = artificialRecordListTable;
		},reSearch:function(){
        	this.datatable.fnDraw();
        },removeClass:function(){//去除Form表单的验证样式
        	$('.form-group','#addForm').removeClass('has-success')
            .removeClass('has-error');
    		$('span').remove('.help-block');
        },initListener:function(){
			/**
			 * 监听开关事件
			 */
        	// 选择员工modal
        	jQuery("#selected").click(function(){
        		$("#groupUserModal").modal('show');
        	});
        	// 查询
        	jQuery("#searchBtn").click(function() { 
        		ArtificialRecord.reSearch();
				return false;
			});
        	// 新增
        	jQuery("#addBtn").click(function() { 
        		ArtificialRecord.removeClass();
        		$("div[name=addDlg] h4.modal-title").html("新增");
        		$("#addForm")[0].reset();
				$("#addDlg").modal("show");
        	});
            // 保存修改后工时
            $("#addFormBtn").click(function(){
				var addForm = $('#addForm');
				addForm.validate({
					rules : {
						emplCode : {
							required : true,
							maxlength : 50
						},clockTime : {
							required : true,
						},clockPlace : {
							required : true,
							maxlength : 50
						}
					}
				});
      			if(addForm.valid()){
      				var postData = $("form[name='addForm']").serializeJson();
      				$.ajax({
      					"dataType" : 'json',
      					"cache": false,
      					"type" : "POST",
      					"data" : postData,
      					"url" : ctx + "/attendanceDetail/add?now="+new Date().getTime(),
      					"success" : function(data) {
      						if(data.type=='success'){
      							$("#addDlg").modal("hide");
      							BootstrapDialog.show({
      								title : messages.public_cue_cue,
      								message : messages.public_cue_saveOk,
      								buttons : [ {
      									label : messages.public_cue_sure,
      									cssClass : 'btn btn-sm btn-primary',
      									action : function(dialogItself) {
      										ArtificialRecord.reSearch();
      										dialogItself.close();
      									}
      								} ]
      							});
      						}else{
      							BootstrapDialog.show({
      								type : BootstrapDialog.TYPE_WARNING,
      								title : messages.public_cue_warning,
      								message : data.message,
      								buttons : [ {
      									label : messages.public_cue_sure,
      									cssClass : 'btn btn-sm btn-primary',
      									action : function(dialogItself) {
      										ArtificialRecord.reSearch();
      										dialogItself.close();
      									}
      								} ]
      							});
      						}
      					}
      				});
      			}
      		});
			
        	// 删除
        	jQuery("#artificialRecordListTable tbody").on("click", "button[name='del']", function() {
				var id = $(this).attr("target_id");
				ArtificialRecord.del(id);
			});
     	},del : function(id) {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : messages.public_cue_warning,
				message : messages.public_cue_delSure,
				buttons : [ {
					label : messages.public_cue_close,
					cssClass: 'btn btn-sm btn-default',
					action : function(dialogItself) {
						dialogItself.close();
					}
				}, {
					label : messages.public_cue_sure,
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						ArtificialRecord.sureDel(id);
						dialogItself.close();
					}
				} ]
			});
		},sureDel : function(id){
			$.ajax({
				"dataType" : 'json',
				"cache": false,
				"type" : "POST",
				"data" : {},
				"url" : ctx + '/attendanceDetail/delete/'+id+'?now='+new Date().getTime(),
				"success" : function(data, textStatus, jqXHR){
					if (data.type == 'success') {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : messages.public_cue_delSuccess,
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									ArtificialRecord.reSearch();
									dialog.close();
								}
							} ]
						});
					} else {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : data.message,
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									dialog.close();
								}
							} ]
						});
					}
				}
			});
		}
	}
}();